package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;

public class EnviromentScrollAdapter  extends AppAdapter<DeviceBean> {
    public EnviromentScrollAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public EnviromentScrollAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EnviromentScrollAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private final ImageView enviromentIcon;
        private final TextView tvEnviromentName;
        private final TextView tvEnviromentUnit;

        private ViewHolder() {
            super(R.layout.enviroment_scroll_item);
            enviromentIcon = findViewById(R.id.image_enviroment_icon);
            tvEnviromentName = findViewById(R.id.tv_enviroment_name);
            tvEnviromentUnit = findViewById(R.id.tv_enviroment_unit);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
//            notifyDataSetChanged();
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            DeviceBean deviceBean = getItem(position);
            if(deviceBean.getDeviceType().equals("temperature_sensor")){
                tvEnviromentName.setText(deviceBean.getStateData().getStatus().getTemperature() + "");
                tvEnviromentUnit.setText("℃");
            }else if(deviceBean.getDeviceType().equals("humidity_sensor")){
                tvEnviromentName.setText(deviceBean.getStateData().getStatus().getHumidity() + "");
                tvEnviromentUnit.setText("%");
            }else if(deviceBean.getDeviceType().equals("illuminance_sensor")){
                tvEnviromentName.setText(deviceBean.getStateData().getStatus().getIlluminance() + "");
                tvEnviromentUnit.setText("lux");
            }else if(deviceBean.getDeviceType().equals("pm2_5_sensor")){
                tvEnviromentName.setText(deviceBean.getStateData().getStatus().getPm2_5() + "");
                tvEnviromentUnit.setText("μg/m³");
            }else if(deviceBean.getDeviceType().equals("noise_sensor")){
                tvEnviromentName.setText(deviceBean.getStateData().getStatus().getNoise() + "");
                tvEnviromentUnit.setText("dB");
            }else if(deviceBean.getType().equals("trigger")){
                tvEnviromentName.setText(LLSceneLinkageUtil.getStateActionName(deviceBean.getEnvironmentType(),deviceBean.getStateData().getStatus().getTrigger()));
                tvEnviromentUnit.setText("");
            }
            GlideApp.with(enviromentIcon).load(deviceBean.getOnImageUrl()).into(enviromentIcon);
        }
    }
}
